package app.sb.jly.com.ji_liang_yuan.activitys.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by leilei on 2021/12/3.
 */

public class MySignView extends View{

    private Bitmap mBitmap;
    private Paint mPaint;
    private Path mPath;
    private Canvas mCanvas;
    private boolean isDraw; //判断路径是否为空
    private int mLastX;
    private int mLastY;


    public MySignView(Context context) {
        super(context);
        init();
    }

    public MySignView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MySignView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPaint = new Paint();
        mPath = new Path();
        isDraw = false;
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeWidth(10);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        //初始化bitmap,canvas
        mBitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(mPath,mPaint);
        canvas.drawBitmap(mBitmap,0,0,null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        isDraw = true;
        int action = event.getAction();
        int x = (int)event.getX();
        int y = (int)event.getY();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                mLastX = x;
                mLastY = y;
                mPath.moveTo(mLastX,mLastY);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                int historySize = event.getHistorySize();
                for (int i = 0; i <historySize ; i++) {
                    float historicalX = event.getHistoricalX(i);
                    float historicalY = event.getHistoricalY(i);
                    mPath.lineTo(historicalX,historicalY);
                }
                int dx = Math.abs(x - mLastX);//取绝对值
                int dy = Math.abs(y - mLastY);
                if (dx >= 3 || dy >= 3){
                    mPath.lineTo(x,y);
                }
                mLastX = x;
                mLastY = y;
                invalidate();
                break;
        }
        return true;
    }

    //返回bitmap
    public Bitmap getBitmap(){
        if (!isDraw){
            return null;
        }
        return mBitmap;
    }

    //清空画布
    public void clear(){
        mPath.reset();
        mBitmap = Bitmap.createBitmap(mCanvas.getWidth(),mCanvas.getHeight(), Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        invalidate();
    }
}
